// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'package:hawk/hawk.dart';
import 'package:nim_core/nim_core.dart';

class HandlePassThroughCase extends HandleBaseCase {
  HandlePassThroughCase();

  @override
  Future<ResultBean?> handle(event) async {
    super.handle(event);
    var handled = false;
    var isCallback = false;
    var result;
    if (className?.trim() != 'PassThroughService') {
      return null;
    }
    switch (methodName?.trim()) {
      case 'httpProxy':
        {
          var map = params![0];
          final ret = await NimCore.instance.passThroughService
              .httpProxy(map['passThroughProxyData']);
          result = ret;
          handled = true;
          handleCaseResult = ResultBean(
            code: result.code,
            data: result.data?.toMap(),
            message: result.errorDetails,
          );
          break;
        }
      case 'onPassThroughNotifyData':
        {
          isCallback = true;
          NimCore.instance.passThroughService.onPassThroughNotifyData
              .listen((event) {
            print('PassThroughService#onPassThroughNotifyData ');
            IntegratedManager.instance.report('onPassThroughNotifyData',
                ResultBean(code: 0, data: event.toMap()));
          });
          break;
        }
    }

    if (isCallback) {
      handleCaseResult = ResultBean.success(data: null);
    }

    // if (handled) {
    //   print('PassThroughService#$methodName result: ${result.code}');
    //   handleCaseResult = ResultBean(
    //     code: result.code,
    //     data: result.data?.toJson(),
    //     message: result.errorDetails,
    //   );
    // }

    return handleCaseResult;
  }
}
